package pe.com.bbva.efcap.servidor.controller;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.RowEditEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import pe.com.bbva.efcap.servidor.bean.DeudaBean;
import pe.com.bbva.efcap.servidor.bean.PeriodoCuentaBean;
import pe.com.bbva.efcap.servidor.bean.ServicioDeudaBean;
import pe.com.bbva.efcap.servidor.modelo.entidad.DeudaInstitucionFinanciera;
import pe.com.bbva.efcap.servidor.servicio.DeudaService;
import pe.com.bbva.efcap.servidor.servicio.ParametroGeneralService;
import pe.com.bbva.efcap.servidor.util.Constante;

@Component("serDeudaMB")
@Scope("view")
public class ServicioDeudaController implements Serializable {

	private static final Logger LOGGER = LoggerFactory.getLogger(ServicioDeudaController.class);
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private DeudaService deudaService;
		
	private Date mesDeuda;
	private BigDecimal totalDeudaBancos;
	private BigDecimal otraDeuda;
	private BigDecimal deudaAccionistas;
	private List<DeudaBean> lDeudasBanco;
	private List<ServicioDeudaBean> lDeudasResumen;
	
	private int item; 
	private String banco;
	private BigDecimal monto;
	private BigDecimal tasa;
	private BigDecimal plazoRestante;
	private BigDecimal periodoGracia;
	
	@PostConstruct
	public void init() {
		try {
			LOGGER.info("ServicioDeudaController -> init");
			lDeudasBanco = new ArrayList<DeudaBean>();
			listarDeudasBanco();
			mesDeuda = new Date();
		} catch (Exception e) {
			LOGGER.debug(Constante.EXECPCION_ENCONTRADA + e.toString(), e);
		}
	}
	
	public void onEdit(RowEditEvent event) {
		FacesMessage msg = new FacesMessage("Registro editado", ((DeudaBean) event.getObject()).getBancoNombre());  
        FacesContext.getCurrentInstance().addMessage(null, msg);
		DeudaBean dbean = (DeudaBean)event.getObject();
		
		ServicioDeudaBean sBean= new ServicioDeudaBean();
		
    }  
	
	public String addAction() {
		DeudaBean bean = new DeudaBean();
		bean.setItem(lDeudasBanco.size()+1);
		bean.setBancoNombre("");
		lDeudasBanco.add(bean);
		return null;
	}
 
	public String deleteAction() {
		String bancoDeleted = (String)FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("bancoDeleted");
		for(DeudaBean bean : lDeudasBanco){
			if(String.valueOf(bean.getItem()).equals(bancoDeleted)){
				lDeudasBanco.remove(bean);
				return null;
			}
		}		
		return null;
	}
	
	public void listarDeudasBanco(){
		int idCliente = 1;
		String tipo = "DEU_BANCOS";
		lDeudasBanco = deudaService.obtenerDeudasCliente(idCliente, tipo);
	}
	
	public void guardar(){
		ServicioDeudaBean bean = new ServicioDeudaBean();
		bean.setMesDeuda(mesDeuda);
		bean.setTotalDeudaBancos(totalDeudaBancos);
		bean.setOtrasDeudas(otraDeuda);
		bean.setDeudasAccionistas(deudaAccionistas);
		bean.setDeudaBancos(lDeudasBanco);
		DeudaInstitucionFinanciera d= new DeudaInstitucionFinanciera();
		long retorno = deudaService.guardarDeuda(d);
		System.out.println(retorno);
	}
	 
	public Date getMesDeuda() {
		return mesDeuda;
	}

	public void setMesDeuda(Date mesDeuda) {
		this.mesDeuda = mesDeuda;
	}

	public BigDecimal getTotalDeudaBancos() {
		return totalDeudaBancos;
	}

	public void setTotalDeudaBancos(BigDecimal totalDeudaBancos) {
		this.totalDeudaBancos = totalDeudaBancos;
	}

	public BigDecimal getOtraDeuda() {
		return otraDeuda;
	}

	public void setOtraDeuda(BigDecimal otraDeuda) {
		this.otraDeuda = otraDeuda;
	}

	public BigDecimal getDeudaAccionistas() {
		return deudaAccionistas;
	}

	public void setDeudaAccionistas(BigDecimal deudaAccionistas) {
		this.deudaAccionistas = deudaAccionistas;
	}

	public List<DeudaBean> getlDeudasBanco() {
		return lDeudasBanco;
	}

	public void setlDeudasBanco(List<DeudaBean> lDeudasBanco) {
		this.lDeudasBanco = lDeudasBanco;
	}

	public int getItem() {
		return item;
	}

	public void setItem(int item) {
		this.item = item;
	}

	public String getBanco() {
		return banco;
	}

	public void setBanco(String banco) {
		this.banco = banco;
	}

	public BigDecimal getMonto() {
		return monto;
	}

	public void setMonto(BigDecimal monto) {
		this.monto = monto;
	}

	public BigDecimal getTasa() {
		return tasa;
	}

	public void setTasa(BigDecimal tasa) {
		this.tasa = tasa;
	}

	public List<ServicioDeudaBean> getlDeudasResumen() {
		return lDeudasResumen;
	}

	public void setlDeudasResumen(List<ServicioDeudaBean> lDeudasResumen) {
		this.lDeudasResumen = lDeudasResumen;
	}

	

		
}
